<?php

namespace App\Http\Controllers\Car;

use App\Helpers\ResponseData;
use App\Http\Controllers\Controller;
use App\Models\Car;
use App\Models\CarCalendar;
use Illuminate\Http\Request;

class CalendarController extends Controller
{
    use ResponseData;
    //            foreach ($data as $k=>$v){
    //                foreach ($mod->calendar as $item){
    //                    if($v['day']==$item->day){
    //                        $data[$k]['day_price'] = $item->day_price;
    //                    }
    //                }
    //            }
    public function list(Request $request){
        $mod = Car::with('calendar')->find($request->id);
        if(count($mod->calendar)>0){
            $data = $mod->calendar;
        }else{
            $data = $this->SenvenDay($mod->minimum_price);
        }
        return $this->success($data);
    }
    protected function delCale($id){
        $cale = CarCalendar::where('car_id',$id);
        $cale->delete();
    }
    public function update(Request $request){
        $mod = Car::find($request->id);
        $this->delCale($request->id);
        $data = json_decode($request->data,true);

        foreach($data as $item){
            $calenar = new CarCalendar(['day'=>$item['day'],'week'=>$item['week'],'day_price'=>$item['day_price']]);
            $mod->calendar()->save($calenar);
        }
        return $this->success('更新成功');
    }
    //删除历史记录
    public function destory($id){
        $mod = CarCalendar::where('car_id',$id);
        return $mod->delete();
    }
}
